package ru.ncedu.java.vlku.hrs.dal.dao.impl;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import ru.ncedu.java.vlku.hrs.dal.dao.DataBaseDAO;
import ru.ncedu.java.vlku.hrs.model.*;
import ru.ncedu.java.vlku.hrs.dal.util.HibernateUtil;

import java.util.ArrayList;
import java.util.List;

public class DataBaseDAOImpl implements DataBaseDAO {
    // CustomerAccount
    public void createCustomerAccount( CustomerAccount ca ) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(ca);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Create customer account error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public void updateCustomerAccount( CustomerAccount ca ) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(ca);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Update customer account error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public CustomerAccount getCustomerAccountById( Long id ) {
        Session session = null;
        CustomerAccount customerAccount = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            customerAccount = (CustomerAccount) session.get(CustomerAccount.class, id);
        } catch (Exception e) {
            System.err.println("Get by id customer account error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return customerAccount;
    }
    public List<CustomerAccount> getCustomerAccountsWithName( String name ) {
        Session session = null;
        List<CustomerAccount> customerAccounts = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            customerAccounts = session.createCriteria(CustomerAccount.class)
                    .add(Restrictions.like("name", name))
                    .list();
        } catch (Exception e) {
            System.err.println("Get by id customer account error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return customerAccounts;
    }
    public List<CustomerAccount> getAllCustomerAccounts() {
        Session session = null;
        List<CustomerAccount> customerAccounts = new ArrayList<CustomerAccount>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            customerAccounts = session.createCriteria(CustomerAccount.class).list();
        } catch (Exception e) {
            System.err.println("Get all customer accounts error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return customerAccounts;
    }

    // Shop
    public void createShop( Shop shop ) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(shop);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Create shop error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public void updateShop( Shop shop ) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(shop);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Update shop error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public Shop getShopById( Long id ) {
        Session session = null;
        Shop shop = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            shop = (Shop) session.get(Shop.class, id);
        } catch (Exception e) {
            System.err.println("Get shop by id error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return shop;
    }
    public List<Shop> getShopsWithName( String name ) {
        Session session = null;
        List<Shop> shops = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            shops = session.createCriteria(Shop.class)
                    .add(Restrictions.like("name", name))
                    .list();
        } catch (Exception e) {
            System.err.println("Get by id customer account error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return shops;
    }
    public List<Shop> getAllShops() {
        Session session = null;
        List<Shop> shop = new ArrayList<Shop>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            shop = session.createCriteria(Shop.class).list();
        } catch (Exception e) {
            System.err.println("Get all shops error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return shop;
    }

    // Storage
    public void createStorage( Storage storage ) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(storage);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Create storage error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public void updateStorage( Storage storage ) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(storage);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Update storage error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public Storage getStorageById( Long id ) {
        Session session = null;
        Storage storage = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            storage = (Storage) session.get(Storage.class, id);
        } catch (Exception e) {
            System.err.println("Get storage by id error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return storage;
    }
    public List<Storage> getStoragesWithName( String name ) {
        Session session = null;
        List<Storage> storages = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            storages = session.createCriteria(Storage.class)
                    .add(Restrictions.like("name", name))
                    .list();
        } catch (Exception e) {
            System.err.println("Get by id customer account error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return storages;
    }
    public List<Storage> getAllStorages() {
        Session session = null;
        List<Storage> storages = new ArrayList<Storage>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            storages = session.createCriteria(Storage.class).list();
        } catch (Exception e) {
            System.err.println("Get all storages error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return storages;
    }

    // Bill
    public void createBill( Bill bill ) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(bill);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Create bill error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public void updateBill( Bill bill ) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(bill);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Update bill error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public Bill getBillById( Long id ) {
        Session session = null;
        Bill bill = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            bill = (Bill) session.get(Bill.class, id);
        } catch (Exception e) {
            System.err.println("Get bill by id error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return bill;
    }
    public List<Bill> getAllBills() {
        Session session = null;
        List<Bill> bills = new ArrayList<Bill>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            bills = session.createCriteria(Bill.class).list();
        } catch (Exception e) {
            System.err.println("Get all bills error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return bills;
    }

    // Tariff
    public void createTariff( Tariff tariff ) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(tariff);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Create tariff error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public void updateTariff( Tariff tariff ) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(tariff);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Update tariff error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public Tariff getTariffById( Long id ) {
        Session session = null;
        Tariff tariff = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tariff = (Tariff) session.get(Tariff.class, id);
        } catch (Exception e) {
            System.err.println("Get tariff by id error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return tariff;
    }
    public List<Tariff> getAllTariffs() {
        Session session = null;
        List<Tariff> tariffs = new ArrayList<Tariff>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tariffs = session.createCriteria(Tariff.class).list();
        } catch (Exception e) {
            System.err.println("Get all tariffs error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return tariffs;
    }

    // Cargo
    public void createCargo( Cargo cargo ) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(cargo);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Create cargo error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public void updateCargo( Cargo cargo ) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(cargo);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Update cargo error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public Cargo getCargoById( Long id ) {
        Session session = null;
        Cargo cargo = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            cargo = (Cargo) session.get(Cargo.class, id);
        } catch (Exception e) {
            System.err.println("Get cargo by id error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return cargo;
    }
    public List<Cargo> getCargosWithName( String name ) {
        Session session = null;
        List<Cargo> cargos = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            cargos = session.createCriteria(Cargo.class)
                    .add(Restrictions.like("name", name))
                    .list();
        } catch (Exception e) {
            System.err.println("Get by id customer account error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return cargos;
    }
    public List<Cargo> getAllCargos() {
        Session session = null;
        List<Cargo> cargos = new ArrayList<Cargo>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            cargos = session.createCriteria(Cargo.class).list();
        } catch (Exception e) {
            System.err.println("Get all cargos error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return cargos;
    }

    //Price
    public void createPrice( Price price ) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(price);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Create cargo error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public void updatePrice( Price price ) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(price);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Update cargo error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public Price getPriceById( Long id ) {
        Session session = null;
        Price price = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            price = (Price) session.get(Price.class, id);
        } catch (Exception e) {
            System.err.println("Get cargo by id error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return price;
    }
    public List<Price> getAllPrices() {
        Session session = null;
        List<Price> prices = new ArrayList<Price>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            prices = session.createCriteria(Price.class).list();
        } catch (Exception e) {
            System.err.println("Get all cargos error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return prices;
    }

    // BillCargo
    public void createBillCargo( BillCargo bc ) {
        // Check by existence matching bill and cargo in tables ???

        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(bc);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Create cargo error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public void updateBillCargo( BillCargo bc ) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(bc);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Update cargo error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public BillCargo getBillCargoById( Long id ) {
        Session session = null;
        BillCargo billcargo = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            billcargo = (BillCargo) session.get(BillCargo.class, id);
        } catch (Exception e) {
            System.err.println("Get cargo by id error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return billcargo;
    }
    public List<BillCargo> getAllBillCargos() {
        Session session = null;
        List<BillCargo> billCargos = new ArrayList<BillCargo>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            billCargos = session.createCriteria(BillCargo.class).list();
        } catch (Exception e) {
            System.err.println("Get all cargos error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return billCargos;
    }

    // ShopCargoAvailability
    public void createShopCargoAvailability( ShopCargoAvailability sca ) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(sca);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Create cargo error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public void updateShopCargoAvailability( ShopCargoAvailability sca ) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(sca);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Update cargo error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public ShopCargoAvailability getShopCargoAvailabilityById( Long id ) {
        Session session = null;
        ShopCargoAvailability sca = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            sca = (ShopCargoAvailability) session.get(ShopCargoAvailability.class, id);
        } catch (Exception e) {
            System.err.println("Get cargo by id error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return sca;
    }
    public List<ShopCargoAvailability> getAllShopCargoAvailabilities() {
        Session session = null;
        List<ShopCargoAvailability> scas = new ArrayList<ShopCargoAvailability>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            scas = session.createCriteria(ShopCargoAvailability.class).list();
        } catch (Exception e) {
            System.err.println("Get all cargos error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return scas;
    }

    // StorCargoAvailability
    public void createStorCargoAvailability( StorCargoAvailability sca ) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(sca);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Create cargo error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public void updateStorCargoAvailability( StorCargoAvailability sca ) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(sca);
            session.getTransaction().commit();
        } catch (Exception e) {
            System.err.println("Update cargo error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    public StorCargoAvailability getStorCargoAvailabilityById( Long id ) {
        Session session = null;
        StorCargoAvailability sca = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            sca = (StorCargoAvailability) session.get(StorCargoAvailability.class, id);
        } catch (Exception e) {
            System.err.println("Get cargo by id error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return sca;
    }
    public List<StorCargoAvailability> getAllStorCargoAvailabilities() {
        Session session = null;
        List<StorCargoAvailability> scas = new ArrayList<StorCargoAvailability>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            scas = session.createCriteria(StorCargoAvailability.class).list();
        } catch (Exception e) {
            System.err.println("Get all cargos error." + e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return scas;
    }
}
